#pragma once

#include <typedef.h>
//
#include <buff_fifo.hpp>

typedef enum
{
	P_TYPE_HEART = 0,
	P_TYPE_FLIGHT_DATA, // 飞控监控数据
	P_TYPE_PARAM_SET,	// 设置参数
	P_TYPE_PARAM_GET,	// 获取参数
	P_TYPE_PARAM_LOAD,	// 从配置文件中载入参数
	P_TYPE_PARAM_SAVE,	// 保存参数到配置文件
	P_TYPE_COMMAND,		// 命令
	P_TYPE_RC,			// 遥控器
} P_TYPE;

#define P_HEAD_0		  0x55
#define P_HEAD_1		  0xAA

#define P_FOOT_0		  0x5A
#define P_FOOT_1		  0xA5

#define P_SIZE_HEAD		  (2)
#define P_SIZE_SEQ		  (2)
#define P_SIZE_TYPE		  (2)
#define P_SIZE_LEN		  (4)
#define P_SIZE_CRC		  (4)
#define P_SIZE_FOOT		  (2)

#define P_PRO_LEN_NO_DATA (P_SIZE_HEAD + P_SIZE_SEQ + P_SIZE_TYPE + P_SIZE_LEN + P_SIZE_CRC + P_SIZE_FOOT)

#define CRC32_SEED_VAL	  (0xEDB88320L)

class Protocol
{
public:
	Protocol();
	virtual ~Protocol();

	int Pack(char* out, int* outSize, char* pData, int dataSize, uint16_t dataSeq, uint16_t dataType);
	int Unpack(char* out, int* outSize, uint16_t* dataSeq, uint16_t* dataType, BuffFifo* buffFifo);

private:
	uint32_t Crc32Value(uint32_t i);
	uint32_t Calc32Crc(char* pData, int size);
};
